package com.example.demo.service.impl;

import com.example.demo.entity.*;
import com.example.demo.mapper.HospitalMapper;
import com.example.demo.service.HospitalService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class HospitalServiceImpl implements HospitalService {
    @Autowired
    private HospitalMapper hospitalMapper;
    @Override
    public List<HospitalTypeInfo> findHospitalTypeInfo() {
        return hospitalMapper.findHospitalTypeInfo();
    }

    @Override
    public List<HospitalAreaInfo> findHospitalAreaInfo() {
        return hospitalMapper.findHospitalAreaInfo();
    }

    @Override
    public List<Hospital> findHospital() {
        return hospitalMapper.findHospital();
    }

    @Override
    public List<HospitalPointRank> findHospitalPointRank() {
        return hospitalMapper.findHospitalPointRank();
    }

    @Override
    public List<HospitalPopularityRank> findHospitalPopularityRank() {
        return hospitalMapper.findHospitalPopularityRank();
    }

    @Override
    public PageInfo<Hospital> selectHospitalPage(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Hospital> hospitals=hospitalMapper.findHospital();
        PageInfo<Hospital> hospitalPageInfo=new PageInfo<Hospital>(hospitals);
        return hospitalPageInfo;
    }

    @Override
    public Hospital findHospitalByName(String name) {
        return hospitalMapper.findHospitalByName(name);
    }

    @Override
    public void insertHospital(Hospital hospital) {
        hospitalMapper.insertHospital(hospital);
        hospitalMapper.addHospitalAreaInfo(hospital.getArea());
        hospitalMapper.addHospitalTypeInfo(hospital.getType());
        hospitalMapper.insertHospitalPointRank(hospital);
        hospitalMapper.insertHospitalPopularityRank(hospital);
    }

    @Override
    public Hospital findHospitalById(int id) {
        return hospitalMapper.findHospitalById(id);
    }

    @Override
    public void updateHospitalEX(Hospital hospital1) {
        hospitalMapper.reduceHospitalAreaInfo(hospital1.getArea());
        hospitalMapper.reduceHospitalTypeInfo(hospital1.getType());
    }

    @Override
    public void updateHospital(Hospital hospital) {
        Hospital hospitaldb=hospitalMapper.findHospitalById(hospital.getId());
        hospitalMapper.updateHospital(hospital);
        hospitalMapper.addHospitalTypeInfo(hospital.getType());
        hospitalMapper.addHospitalAreaInfo(hospital.getArea());
        HospitalPointRank hospitalPointRank=hospitalMapper.findHospitalPointRankByname(hospitaldb.getName());
        HospitalPopularityRank hospitalPopularityRank=hospitalMapper.findHospitalPopularityRankByname(hospitaldb.getName());
        if (hospitalPointRank!=null){
            hospital.setId(hospitalPointRank.getId());
            hospitalMapper.updateHospitalPointRank(hospital);

        }
        if (hospitalPopularityRank!=null){
            hospital.setId(hospitalPopularityRank.getId());
            hospitalMapper.updateHospitalPopularityRank(hospital);
        }
    }

    @Override
    public void deleteHospital(int id) {
        Hospital hospital=hospitalMapper.findHospitalById(id);
        hospitalMapper.deleteHospitalById(id);
        hospitalMapper.reduceHospitalAreaInfo(hospital.getArea());
        hospitalMapper.reduceHospitalTypeInfo(hospital.getType());
        hospitalMapper.deleteHospitalPointRankByName(hospital.getName());
        hospitalMapper.deleteHospitalPopularityRankByName(hospital.getName());
    }
}
